package settings.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import settings.DAO.UserDao;
import settings.domain.User;
import settings.exception.LoginException;
import settings.service.UserService;
import util.DateUtil;
import util.MD5Util;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    public User userLoginCheckService(String loginAct,String loginPwd,String ip) throws LoginException{
        //密码加密
        loginPwd= MD5Util.getMD5(loginPwd);
        User loginMsg=new User();
        loginMsg.setLoginAct(loginAct);
        loginMsg.setLoginPwd(loginPwd);
        User user=userDao.selectUserByActAndPwd(loginMsg);
        if(user==null){
            throw new LoginException("用户密码或者账号有误");
        }
        String lockState=user.getLockState();
        String expireTime=user.getExpireTime();
        String allowIps=user.getAllowIps();
        if(!allowIps.contains(ip)){
            throw new LoginException("该ip地址不被允许访问");
        }
        String curTime=DateUtil.dateToString19Char(new Date(System.currentTimeMillis()));
        if(expireTime.compareTo(curTime)<0){
            throw new LoginException("该账号已失效");
        }
        if("0".equals(lockState)){
            throw new LoginException("该账号已被锁定");
        }
        return user;
    }
    public List getAllUser(){
        return userDao.getUserList();
    }
}
